package com.csii.adhoc.board.serviceImpl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csii.adhoc.board.BoardDtlPo;
import com.csii.adhoc.board.BoardDtlVo;
import com.csii.adhoc.board.filter.FilterUnitVo;
import com.csii.adhoc.board.mapper.BoardDtlMapper;
import com.csii.adhoc.board.mapper.DashBoardMapper;
import com.csii.adhoc.board.service.BoardDtlService;
import com.csii.adhoc.board.service.DashBoardService;
import com.csii.adhoc.board.service.FilterUnitService;
import com.csii.adhoc.dataready.TMgnDir;
import com.csii.adhoc.dataunits.UnitBase.UnitBasePo;
import com.csii.adhoc.dataunits.service.UnitBaseService;
import com.csii.adhoc.exception.ParamValidityException;
import com.csii.adhoc.id.UidGenerator;
import com.csii.adhoc.util.ParamCheckUtils;
import com.csii.common.constant.CommonConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @description:
 * @author: panChuanJiang
 * @create: 2021-02-03 10:29
 */
@Service("boardDtlService")
@Slf4j
public class BoardDtlServiceImpl implements BoardDtlService {
    @Resource
    private BoardDtlMapper boardDtlMapper;
    @Resource
    private FilterUnitService filterUnitService;
    @Resource
    private UidGenerator uidGenerator;
    @Resource
    private UnitBaseService unitBaseService;

    @Transactional
    @Override
    public void saveListPanelInfo(List<BoardDtlPo> list) {
        if (ParamCheckUtils.checkListIsEmpty(list)) {
            return;
        }
        boardDtlMapper.updateListBoardDtlByPrimaryKey(list);
    }

    @Override
    public List<BoardDtlVo> queryPanelInfo(BoardDtlPo boardDtlPo) {
        return boardDtlMapper.selectListBoardDtl(boardDtlPo);
    }

    @Override
    @Transactional
    public String boardDtlMaintain(BoardDtlPo boardDtlPo, String optMode) {
        switch (optMode) {
            case CommonConstant.OPT_MODE_ADD:
                boardDtlPo.setCreatedBy(boardDtlPo.getUpdatedBy())
                        .setCreatedDt(boardDtlPo.getUpdatedDt());
                if (StringUtils.isEmpty(boardDtlPo.getUnitId())) {
                    boardDtlPo.setUnitId(uidGenerator.getUID() + "");
                }
                insertOneBoardDtl(boardDtlPo);
                return boardDtlPo.getUnitId();
            case CommonConstant.OPT_MODE_DELETE:
                deleteBoardDtlByPrimaryKey(boardDtlPo);
                return boardDtlPo.getUnitId();
            case CommonConstant.OPT_MODE_UPDATE:
                updateBoardDtlByPrimaryKey(boardDtlPo);
                return boardDtlPo.getUnitId();
            default:
                log.error("optMode值不合法:" + optMode);
                throw new ParamValidityException("optMode值不合法:" + optMode);
        }
    }

    @Transactional
    public void updateBoardDtlByPrimaryKey(BoardDtlPo boardDtlPo) {
        boardDtlMapper.updateBoardDtlByPrimaryKey(boardDtlPo);
    }

    @Transactional
    public void deleteBoardDtlByPrimaryKey(BoardDtlPo boardDtlPo) {
        BoardDtlPo po = boardDtlMapper.selectById(boardDtlPo.getUnitId());
        if ("1".equals(po.getUnitType())) {
            UnitBasePo unitBasePo = new UnitBasePo();
            unitBasePo.setUnitId(boardDtlPo.getUnitId());
            unitBaseService.unitBaseMaintain(unitBasePo, CommonConstant.OPT_MODE_DELETE);
        } else if ("2".equals(po.getUnitType())) {
            FilterUnitVo vo = new FilterUnitVo();
            vo.setUnitId(boardDtlPo.getUnitId());
            filterUnitService.FilterUnitMaintain(vo, CommonConstant.OPT_MODE_DELETE);
        }
        boardDtlMapper.deleteBoardDtlByPrimaryKey(boardDtlPo);
    }

    @Transactional
    @Override
    public void insertOneBoardDtl(BoardDtlPo boardDtlPo) {
        if ("1".equals(boardDtlPo.getUnitType())) {
            UnitBasePo unitBasePo = new UnitBasePo();
            unitBasePo.setUnitId(boardDtlPo.getUnitId())
                    .setUpdatedDt(boardDtlPo.getUpdatedDt())
                    .setUpdatedBy(boardDtlPo.getUpdatedBy())
                    .setCreatedBy(boardDtlPo.getCreatedBy())
                    .setCreatedDt(boardDtlPo.getCreatedDt())
                    .setHaxis(new BigDecimal(200))
                    .setLength(new BigDecimal(200))
                    .setVaxis(new BigDecimal(200))
                    .setWidth(new BigDecimal(200))
                    .setBoardId(boardDtlPo.getBoardId())
                    .setUnitName("未命名")
                    .setTableType("")
                    .setUnitType("1")
                    //.setSeqno(new BigDecimal(1))
                    .setModelId("")
                    .setTableType("")
                    .setPicType("")
                    .setPicClass("");
            unitBaseService.insertOneUnitBase(unitBasePo);
        }
        boardDtlMapper.insertOneBoardDtl(boardDtlPo);
    }


    /**
     * @description:
     * @author:
     * @create:
     */
    @Service
    public static class DashBoardServiceImpl extends ServiceImpl<DashBoardMapper, TMgnDir> implements DashBoardService {
    }
}
